import { ref } from 'vue'
import { defineStore } from 'pinia'
import { message } from 'ant-design-vue'
import { getLoginUserUsingGet } from '@/api/userController'

// 显式声明 store 的类型
export const useUserLoginUserStore = defineStore('loginUser', () => {
  const loginUser = ref<API.LoginUserVO>({
    userName: '未登录',
  })
  /**
   * 获取当前登录用户
   */
  const getLoginUser = async () => {
    const res = await getLoginUserUsingGet()
    if (res.data.code === 0 && res.data.data) {
      loginUser.value = res.data.data
    } else {
      message.error(res.data.message)
    }
  }
  const setLoginUser = async (newLoginUser: API.User) => {
    loginUser.value = newLoginUser
  }

  return { loginUser, getLoginUser, setLoginUser }
})
